#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 5   //定义栈中元素的最大个数
typedef struct stack {
	int data[MaxSize];  //存放栈中元素
	int top;			//栈顶指针
}SqStack;
void InitStack(SqStack *S){
	S->top = -1;
}
bool StackEmpty(SqStack *S){
	if(S->top == -1){
		return true;
	}
	else {
		return false;
	}
}
bool Push(SqStack *S,int x){
	if(S->top == MaxSize-1){  //栈满
		return false;
	}
	S->data[++S->top] = x;
	return true;
}
bool Pop(SqStack *S,int *x){
	if(StackEmpty(S)){
		return false;
	}
	*x = S->data[S->top--];
	return true;
}
bool GetTop(SqStack *S,int *x){
	if(StackEmpty(S)){
		return false;
	}
	*x = S->data[S->top]; //不减减
	return true;
}
int main(int argc, char *argv[]) {
	SqStack *stack = (SqStack*) malloc(sizeof(SqStack));
	InitStack(stack);
	int *x;
	int choose;
	printf("1.入栈 2.出栈 3.获取栈顶元素:");
	while(scanf("%d",&choose)!=EOF && choose!=-1){
	if(choose == 1){
		int data;
		printf("输入入栈元素:");
		scanf("%d",&data);
		if(Push(stack, data)){
			printf("入栈成功！\n");
		}
		else{
			printf("入栈失败！栈已满\n");
		}
	}
	else if(choose == 2) {
		if(Pop(stack, x)){
			printf("出栈元素为:%d\n",*x);
		}
		else {
			printf("操作失败！栈已空！\n");
		}
	}
	else if(choose == 3) {
		if(GetTop(stack, x)){
			printf("栈顶元素为:%d\n",*x);
		}
		else {
			printf("操作失败！栈已空！\n");
		}
	}
	printf("1.入栈 2.出栈 3.获取栈顶元素:");
	}
}